package com.sg.plugin.bff.filter;

import com.alibaba.fastjson.JSONObject;
import com.wicket.okrbff.common.baseinfo.AuthContext;
import com.wicket.okrbff.common.baseinfo.BaseInfoDO;
import com.wicket.okrbff.common.baseinfo.BaseInfoHolder;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;


/**
 * @author ：CHS
 * @version 1.0
 * @since 2022/5/9 13:53
 */
@Component
@WebFilter(urlPatterns = "/**")
public class RecordChannelFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
        if (path.equals("/oms/common/addUploadFile")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        RequestWrapper requestWrapper = null;
        if (servletRequest instanceof HttpServletRequest) {
            requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
            setPagInfo(requestWrapper.getBody());
        }
        if (requestWrapper == null) {
            //防止流读取一次就没有了,将流传递下去
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }


    }

    @Override
    public void destroy() {
    }

    private void setPagInfo(String body) {
        try {
            if (body == null || body.isEmpty()) return;
            JSONObject jo = JSONObject.parseObject(body);
            if (jo != null && jo.getString("pageNum") != null && jo.getString("pageSize") != null) {
                if (BaseInfoHolder.contextHolder.get() != null) {
                    AuthContext context = BaseInfoHolder.contextHolder.get();
                    BaseInfoDO baseInfo = context.getBaseInfo();
                    baseInfo.setPageNum(Integer.valueOf(jo.getString("pageNum")));
                    baseInfo.setPageSize(Integer.valueOf(jo.getString("pageSize")));
                    if (jo.getString("startTime") != null && !jo.getString("startTime").isEmpty()) {
                        baseInfo.setSpecStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(jo.getString("startTime")));
                    }

                    if (jo.getString("endTime") != null && !jo.getString("endTime").isEmpty()) {
                        baseInfo.setSpecEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(jo.getString("endTime")));
                    }

                } else {
                    AuthContext context = new AuthContext();
                    BaseInfoDO baseInfo = new BaseInfoDO();
                    baseInfo.setPageNum(Integer.valueOf(jo.getString("pageNum")));
                    baseInfo.setPageSize(Integer.valueOf(jo.getString("pageSize")));
                    if (jo.getString("startTime") != null && !jo.getString("startTime").isEmpty()) {
                        baseInfo.setSpecStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(jo.getString("startTime")));
                    }

                    if (jo.getString("endTime") != null && !jo.getString("endTime").isEmpty()) {
                        baseInfo.setSpecEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(jo.getString("endTime")));
                    }
                    context.setBaseInfo(baseInfo);
                    BaseInfoHolder.contextHolder.set(context);
                }
            }
        } catch (Exception e) {

        }

    }
}
